package cn.iocoder.yudao.module.brokerage.user.service;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.business.basic.dao.query.CoreQueryWrapper;
import cn.iocoder.yudao.framework.business.basic.pojo.vo.UserSimpleVO;
import cn.iocoder.yudao.framework.business.basic.service.impl.MemberScopeService;
import cn.iocoder.yudao.framework.qrcode.pojo.QrCodeInviteType;
import cn.iocoder.yudao.framework.qrcode.util.QrCodeUtils;
import cn.iocoder.yudao.module.api.member.user.MemberUserApi;
import cn.iocoder.yudao.module.brokerage.user.convert.BrokerageUserConvert;
import cn.iocoder.yudao.module.brokerage.user.dao.BrokerageUserMapper;
import cn.iocoder.yudao.module.brokerage.user.mq.BrokerageUserProducer;
import cn.iocoder.yudao.module.brokerage.user.pojo.po.BrokerageUserDO;
import cn.iocoder.yudao.module.brokerage.user.pojo.vo.AdminBrokerageUserQueryDTO;
import cn.iocoder.yudao.module.brokerage.user.pojo.vo.AdminBrokerageUserVO;
import jakarta.annotation.Nonnull;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.util.*;

import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

/**
 * 推广用户 Service 实现类
 *
 * @author 山野羡民
 */
@Slf4j
@Service
@Validated
public class AdminBrokerageUserServiceImpl extends MemberScopeService<BrokerageUserDO, AdminBrokerageUserVO, AdminBrokerageUserQueryDTO> implements AdminBrokerageUserService {
    @Resource
    private BrokerageUserMapper brokerageUserMapper;

    @Resource
    private MemberUserApi memberUserApi;

    @Resource
    private BrokerageUserProducer brokerageUserProducer;

    @Override
    protected BrokerageUserMapper getInjectedMapper() {
        return brokerageUserMapper;
    }

    @Nonnull
    @Override
    protected CoreQueryWrapper<BrokerageUserDO> buildQueryWrapper(AdminBrokerageUserQueryDTO param) {
        CoreQueryWrapper<BrokerageUserDO> wrapper = super.buildQueryWrapper(param);
        if (param == null) {
            return wrapper;
        }
        Set<Long> userIds = memberUserApi.getUserIdsByMobileAndNick(param.getMobile(), param.getNickname());
        wrapper.in(CollUtil.isNotEmpty(userIds), "user_id", userIds);
        wrapper.eqIfPresent("type", param.getType());
        wrapper.eqIfPresent("father_user_id", param.getFatherUserId());
        wrapper.orderByDesc("id");
        return wrapper;
    }

    @Nonnull
    @Override
    protected Map<Long, UserSimpleVO> getUserSimpleMap(Collection<Long> userIds) {
        return memberUserApi.getUserSimpleMap(userIds);
    }

    @Override
    public List<AdminBrokerageUserVO> convertToViewObject(@Nonnull List<BrokerageUserDO> entities) {
        if (CollUtil.isEmpty(entities)) {
            return Collections.emptyList();
        }
        List<AdminBrokerageUserVO> vos = BrokerageUserConvert.INSTANCE.convertList2(entities);
        Set<Long> userIds = new HashSet<>();
        userIds.addAll(convertSet(entities, BrokerageUserDO::getUserId));
        userIds.addAll(convertSet(entities, BrokerageUserDO::getFatherUserId));
        Map<Long, UserSimpleVO> users = getUserSimpleMap(userIds);
        for (AdminBrokerageUserVO vo : vos) {
            UserSimpleVO userSimpleVO = users.getOrDefault(vo.getUserId(), UserSimpleVO.ANONYMOUS);
            vo.setUser(userSimpleVO);
            vo.setFatherUser(users.getOrDefault(vo.getFatherUserId(), UserSimpleVO.ANONYMOUS));
            QrCodeInviteType qrCodeInviteType = new QrCodeInviteType();
            qrCodeInviteType.setMemberNo(userSimpleVO.getNo());
            vo.setQrcode(QrCodeUtils.generatePngAsBase64Uri(qrCodeInviteType, userSimpleVO.getAvatar()));
        }
        return vos;
    }

    @Override
    public boolean updateEnabled(Long id, Boolean enabled) {
        BrokerageUserDO entity = validateExists(id);
        entity.setEnabled(enabled);
        brokerageUserMapper.updateById(entity);
        return true;
    }

    @Override
    public boolean clearFatherAndGrandpa(Long id) {
        BrokerageUserDO entity = validateExists(id);
        entity.setFatherUserId(0L);
        entity.setGrandpaUserId(0L);
        brokerageUserMapper.updateById(entity);
        brokerageUserProducer.sendUserUnbindMessage(entity.getUserId(), entity.getFatherUserId());
        return true;
    }

}